package com.ls.smartcampus2.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ls.smartcampus2.pojo.Course;
import com.ls.smartcampus2.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author dell
 */
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/getCourse")
    public String getCourse(String sid,int curWeek) throws JsonProcessingException {
        ObjectMapper mapper=new ObjectMapper();
        List<Course>course;
         if((curWeek%2)==0){
              course = courseService.getDoubleCourse(sid);
         }else{
             course = courseService.getOneCourse(sid);
         }

        String s = mapper.writeValueAsString(course);
        return s;

    }
@GetMapping("/getCourseByDay")
    public String getCourseByDay(String sid) throws JsonProcessingException {
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        weekday=weekday-1;
        String day=String.valueOf(weekday);
        List<Course> courseByDay = courseService.getCourseByDay(sid,day);
        ObjectMapper mapper=new ObjectMapper();
        return mapper.writeValueAsString(courseByDay);



    }




}
